package com.asohofrucol.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.asohofrucol.dao.ColDepartamentDao;
import com.asohofrucol.dao.ColMunicipalityDao;
import com.asohofrucol.dao.ColVillageDao;
import com.asohofrucol.dao.OptionDao;
import com.asohofrucol.dto.ParameterDTO;
import com.asohofrucol.dto.ParameterSiteDTO;
import com.asohofrucol.dto.util.ParameterHelperDto;
import com.asohofrucol.model.ColDepartament;
import com.asohofrucol.model.ColMunicipality;
import com.asohofrucol.model.ColVillage;
import com.asohofrucol.model.Option;
import com.asohofrucol.service.ParameterService;

/**
 * @author Iván
 * 
 */
@Component
public class ParameterServiceImpl implements ParameterService, Serializable {

	/**
	 * 
	 */
	public static String SEPARATOR =",";
	private static final long serialVersionUID = 1L;
	@Autowired
	private OptionDao optionDao;
	@Autowired
	private ColDepartamentDao colDepartamentDao;
	@Autowired
	private ColMunicipalityDao colMunicipalityDao;
	@Autowired
	private ColVillageDao colVillageDao;

	@Override
	public ParameterDTO findByField(String idField) {
		List<String> idfields = new ArrayList<String>();
		String[] idsArray = idField.split(SEPARATOR);
		for(int i =0;i<idsArray.length;i++){
			idfields.add(idsArray[i]);
		}			
		List<Option> options = optionDao.findByField(idfields);
		ParameterDTO parameterDTO = new ParameterDTO();
		parameterDTO.setIdField(idField);
		if (options != null && !options.isEmpty()) {
			parameterDTO = ParameterHelperDto.convertToParameterDTO(options);
		}
		return parameterDTO;
	}

	@Override
	public ParameterSiteDTO getDepartaments() {
		List<ColDepartament> liColDep = colDepartamentDao.findAll();
		ParameterSiteDTO psDto = new ParameterSiteDTO();
		psDto.setCode("57");
		psDto = ParameterHelperDto
				.convertColDepartamentToParameterSiteDTO(liColDep);
		return psDto;
	}

	@Override
	public ParameterSiteDTO getMunicipalitysByDepartament(String codDepartament) {
		List<ColMunicipality> liColMun = colMunicipalityDao
				.listByCodDepartament(codDepartament);
		ParameterSiteDTO psDto = new ParameterSiteDTO();
		psDto.setCode(codDepartament);
		psDto = ParameterHelperDto
				.convertColMunicipalityToParameterSiteDTO(liColMun);
		return psDto;
	}

	@Override
	public ParameterSiteDTO getVillagesByMunicipality(String codMunicipality) {
		List<ColVillage> liColVill = colVillageDao
				.listByCodMunicipality(codMunicipality);
		ParameterSiteDTO psDto = new ParameterSiteDTO();
		psDto.setCode(codMunicipality);
		psDto = ParameterHelperDto
				.convertColVillageToParameterSiteDTO(liColVill);
		return psDto;
	}

}
